package ar.edu.uba.fi.jmit.monitorengine.backend.server;

import java.util.LinkedList;
import java.util.Map;

import ar.edu.uba.fi.jmit.monitorengine.backend.client.VisualizersService;
import ar.edu.uba.fi.jmit.monitorengine.backend.shared.dto.VisualizerDTO;
import ar.edu.uba.fi.jmit.monitorengine.core.ConfigurationManager;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class VisualizersServiceImpl extends RemoteServiceServlet implements VisualizersService {

	private static final long serialVersionUID = 986953714246385596L;
	private static final String PROTOCOL_CONNECTION_TAG = "protocol";
	private static final String IP_CONNECTION_TAG = "ip";
	private static final String PORT_CONNECTION_TAG = "port";
	private static final String CONTEXT_CONNECTION_TAG = "context";
	
	@Override
	public LinkedList<VisualizerDTO> getLoadedVisualizers() {
		LinkedList<VisualizerDTO> visualizers = new LinkedList<VisualizerDTO>();
				
		for(String key : ConfigurationManager.getInstance().getVisualizersProps().keySet()){
			VisualizerDTO dto = new VisualizerDTO();
			dto.setName(key);
			Map<String,String> props = ConfigurationManager.getInstance().getVisualizerProps(key);
			String protocol = props.get(PROTOCOL_CONNECTION_TAG);
			String ip = props.get(IP_CONNECTION_TAG);
			String port = props.get(PORT_CONNECTION_TAG);
			String context = props.get(CONTEXT_CONNECTION_TAG);
			if(!context.startsWith("/")) {
				context = "/" + context;
			}
			dto.setConnectionUrl(protocol + "://" + ip + ":" + port + context);
			dto.setMonitors(ConfigurationManager.getInstance().getMonitors(key));
			visualizers.add(dto);
		}
		
		return visualizers;
	}

}
